import requests
from demo_Requests.pdbc import exeSql


class AddCustomerTest:
    def __init__(self):
        self.host = 'localhost'
        self.port = 8081
        self.s = requests.Session()

    def login(self):
        data = 'username=admin&password=admin123&verifycode=0000'
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        r = self.s.post('http://localhost:8081/WoniuSales1.4/user/login', data=data, headers=headers)
        # print(r.text)

    def add_customer(self, data):
        self.login()
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        r = self.s.post('http://localhost:8081/WoniuSales1.4/customer/add', data=data, headers=headers)
        return r.text

    def test_add(self, data, expctedRes):
        actualRes = self.add_customer(data)
        # 对响应正文断言
        print('########## 开始测试 ##########')
        if actualRes == expctedRes:
            print('测试用例通过!')
        else:
            print('测试用例失败!')
            print('响应: \n预期 - %s, \n实际 - %s' % (expctedRes, actualRes))
        print('########## 结束测试 ##########')

    # 测试用例: 所有参数都正确
    def test_add_param_correct(self):
        data = {'customername': '小王子11', 'customerphone': '13800001127', 'childsex': '男', 'childdate': '2018-10-09',
                'creditkids': '11', 'creditcloth': '22'}
        expectRes = 'add-successful'
        self.test_add(data, expectRes)

        # 清理数据, 保证代码的重用性
        sql = "DELETE FROM customer WHERE customerphone = '13800001127'"
        exeSql(sql)

    # 测试用例: 手机号重复
    def test_add_phone_dup(self):
        data = {'customername': '小王子', 'customerphone': '13800001111', 'childsex': '男', 'childdate': '2018-10-09',
                'creditkids': '11', 'creditcloth': '22'}
        expectRes = 'already-added'
        self.test_add(data, expectRes)

    # 测试用例: 缺少姓名
    def test_add_name_missing(self):
        data = {'customerphone': '13800001111', 'childsex': '男', 'childdate': '2018-10-09',
                'creditkids': '11', 'creditcloth': '22'}
        expectRes = 'add-successful'
        self.test_add(data, expectRes)


if __name__ == '__main__':
    act = AddCustomerTest()
    act.test_add_param_correct()
    act.test_add_phone_dup()
    act.test_add_name_missing()
